import { observable, action, autorun, toJS } from 'mobx';
import * as  Apis from '../../../apis/project';
import { message } from 'antd';
import { WeaLocaleProvider } from "ecCom"
const getLabel = WeaLocaleProvider.getLabel;

export class StageSetStore {

	@observable loading = false;
    @observable prjid = "";
    @observable rightMenu = [];
	@observable stageInfo = {
		stageColumns: [],
		stageColumnDatas: [],
		selectedRowKeys: [],
		selectedDatas: {}
    }

    initData = (prjid) => {
		this.prjid = prjid;
        this.getPrjTempletStageList();
    }

    onRowSelect = (sRowKeys, rows, dataIndex, selectedDatas) => {
		this.stageInfo.selectedRowKeys = sRowKeys;
		if (selectedDatas) {
			this.stageInfo.selectedDatas = selectedDatas;
		}
    }
    
    onChange = (datas) => {
		this.stageInfo.stageColumnDatas = datas;
    }
    
    getPrjTempletStageList = (params = {}) => {
		this.loading = true;
		let newParams = { prjid: this.prjid, ...params };
		Apis.getPrjTempletStageList(newParams).then(data => {
			this.stageInfo.stageColumns = data.columns;
			this.stageInfo.stageColumnDatas = data.columnDatas;
			this.rightMenu = data.rightMenus;
			this.loading = false;
		})
    }
    
    
	saveStageSet = (checkProps) => {
		const { pass } = checkProps;
		if (pass&&this.checkRepeat()) {
			let keepgroupids = "";
			this.stageInfo.stageColumnDatas.forEach(element => {
				if (element.id) {
					keepgroupids += "," + element.id
				}
			});
			keepgroupids = keepgroupids.substr(1, keepgroupids.length);
			Apis.savePrjTempletStage({ datas: JSON.stringify(this.stageInfo.stageColumnDatas), keepgroupids: keepgroupids, prjid: this.prjid }).then(data => {
				if (data.success) {
					this.clearStageStatus();
					message.success(data.msg);
					this.getPrjTempletStageList();
				} else {
					message.error(data.msg);
				}
			})
		}
	}

	checkRepeat = () => {
		let repeat = true;
		let datas = toJS(this.stageInfo.stageColumnDatas);
		datas && datas.length>0 && datas.map((d1,i1) => {
			if(repeat){
				datas && datas.length>0 && datas.map((d2,i2) => {
					if(i1 !== i2 && d1.name == d2.name){
						message.warning(getLabel('387703',"阶段名称重复！"));
						repeat =  false;
					}
				})
			}
		})
		return repeat;
	}

	clearStageStatus = () => {
		this.stageInfo = {
			stageColumns: [],
			stageColumnDatas: [],
			selectedRowKeys: [],
			selectedDatas: {}
		}
	}
	
}

